package com.dowsure.apisaas.api;

import com.dowsure.apisaas.enums.BankCardTypeEnums;
import com.dowsure.apisaas.enums.MethodEnums;
import com.dowsure.apisaas.enums.OverdueStatusEnums;
import com.dowsure.apisaas.exception.ApiException;
import com.dowsure.apisaas.model.PspRepaymentCancelRequestParams;
import com.dowsure.apisaas.model.PspRepaymentDataRequestParams;
import com.dowsure.apisaas.model.PspRepaymentDataResponseParams;
import com.dowsure.apisaas.model.PspRepaymentRequestParams;
import com.dowsure.apisaas.util.ApiClient;
import com.dowsure.apisaas.util.ApiResponse;
import com.dowsure.apisaas.util.RequestParamValidatedUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: psp
 * @description:
 * @author: Dowsure
 * @create: 2022-02-15 18:40
 **/
public class PspApi {

    private ApiClient apiClient;

    public PspApi(ApiClient apiClient){
        this.apiClient = apiClient;
    }


    /**
     * 16、请求强制划扣（API-D02）
     * 当卖家产生逾期时，用户请求Dowsure通知支付公司进行强制结汇处理，支付公司将在 T+1 日响应强制结汇任务，
     * 对商户的资金进行强制结汇至指定的商户银行卡中，若存在当前资金不足的情况下，会定时发起结汇任务，
     * 直到足额处理，足额处理完毕之后会自动关闭强制结汇。Dowsure将结汇任务异步通知用户。
     *  a.需要提供强制划扣卡号和金额信息
     *  b.支付公司完成足额划扣后，自动解除强制划扣功能，无需调用 解除强制划扣（API13）
     * @author Dowsure
     * @param pspRepaymentRequestParams {@link List< PspRepaymentRequestParams >}
     * @return {@link ApiResponse <Void>}
     * @throws ApiException
     */
    public ApiResponse<Void> repayment(List<PspRepaymentRequestParams> pspRepaymentRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(pspRepaymentRequestParams);
        for (PspRepaymentRequestParams params : pspRepaymentRequestParams){
            if (BankCardTypeEnums.ENTERPRISE.equals(params.getCardType())){
                RequestParamValidatedUtils.notBlank(params.getOpenBankName(), "openBankName（支行名称）");
                RequestParamValidatedUtils.notBlank(params.getOpenBankNo(), "openBankNo（大额行号）");
            }
            if (OverdueStatusEnums.NOT_OVERDUE.equals(params.getOverdueStatus())){
                RequestParamValidatedUtils.notBlank(params.getReason(), "reason（划扣原因）");
            }
        }
        String localVarPath = "/v1/psp/repayment";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, pspRepaymentRequestParams, headerParams);
        return apiClient.execute(call);
    }


    /**
     * 17、解除强制划扣（API-D03）
     * 卖家通过其他方式足额还款后，用户需要立即调用此接口解除划扣，Dowsure将解除结果异步通知用户。
     *  需要提供业务ID
     * @author Dowsure
     * @param pspRepaymentCancelRequestParams {@link PspRepaymentCancelRequestParams}
     * @return {@link ApiResponse <Void>}
     * @throws ApiException
     */
    public ApiResponse<Void> repaymentCancel(List<PspRepaymentCancelRequestParams> pspRepaymentCancelRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(pspRepaymentCancelRequestParams);
        String localVarPath = "/v1/psp/repayment-cancel";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, pspRepaymentCancelRequestParams, headerParams);
        return apiClient.execute(call);
    }


    /**
     * 18、获取强制划扣结果（API-D04）
     * 用户可以自主查询对卖家的强制划扣数据。
     *  需要提供业务ID
     * @author Dowsure
     * @param pspRepaymentDataRequestParams {@link PspRepaymentDataRequestParams}
     * @return {@link ApiResponse <  PspRepaymentDataResponseParams  >}
     * @throws ApiException
     */
    public ApiResponse<PspRepaymentDataResponseParams> repaymentData(PspRepaymentDataRequestParams pspRepaymentDataRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(pspRepaymentDataRequestParams);
        String localVarPath = "/v1/psp/repayment-data";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, pspRepaymentDataRequestParams, headerParams);
        return apiClient.execute(call);
    }

}
